$(function() {
	
	$.getJSON("admin/department/department!gets", {
		"listAll" : true
	}, function(data, textStatus, jqXHR) {
		var cnar = $("#groups");
		$.each(data, function(index, item) {
			var opt = $("<option></option>");
			opt.val(item.id);
			opt.text(item.name);
			cnar.append(opt);
		});
	});
	$.getJSON("admin/role/role!gets", {
		"listAll" : true
	}, function(data, textStatus, jqXHR) {
		var cnar = $("#roles");
		$.each(data, function(index, item) {
			var opt = $("<option></option>");
			opt.val(item.id);
			opt.text(item.name);
			cnar.append(opt);
		});
	});
	$.getJSON("fileflow/group/custom-organization!gets", {
		"listAll" : true
	}, function(data, textStatus, jqXHR) {
		var cnar = $("#custs");
		$.each(data, function(index, item) {
			var opt = $("<option></option>");
			opt.val(item.id);
			opt.text(item.name);
			cnar.append(opt);
		});
	});
    $("form button").click(function(){
     
    	var passed = checkForm();
        
    	if(passed){
    	  document.getElementById("fileForm").submit();
    	}
    });
});
function pickParams(){
	var groups = $("#groups option:selected");
	var par = "";
	$.each(groups,function(index,group){
		par+="&bean.departments["+index+"].id="+group.value;
		par+="&bean.departments["+index+"].name="+group.text;
		
	});
	var roles = $("#roles option:selected");
	
	$.each(roles, function(index, role){
        par+="&bean.roles["+index+"].id="+role.value;
        par+="&bean.roles["+index+"].name="+role.text;
	});
	
	var custxx = $("#custs");
	if(custxx.val()!=null)
	par+='bean.customOrganization.id='+custxx.val();
	return par;
}
function checkForm(){
   var f=	$("#ufile");
   var uploaded = $("#uploaded").val();
   var  params = pickParams();
   if(uploaded=='true'){
	   if((f.val()=="" || typeof(f.val())=='undefined')){
		   params+="&uploaded=true";   
		   createNotify("请选择文件....");
		   return false;
	   }
   }else{
	   var entityId = $("#workflowId").val();
	   params+="&uploaded=false&workflowId="+entityId;  
   } 
      
   var act = "fileflow/file/fileflow!save?"+params;
   $("#fileForm").attr("action",act);
   return true;
}